package com.huawei.esdk.tp.device.terminal;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.commu.itf.ISDKProtocolAdapter;
import com.huawei.esdk.platform.exception.ProtocolAdapterException;
import com.huawei.esdk.platform.nemgr.itf.IDevice;
import com.huawei.esdk.tp.device.aa.TE30V1R1C01AAService;
import com.huawei.esdk.tp.device.aa.TerminalAAService;

public class TE30V1R1C01Connection extends TerminalConnection
{
	private static final Logger LOGGER = Logger
			.getLogger(TE30V1R1C01Connection.class);

	private TerminalAAService terminalAAService;

	public TE30V1R1C01Connection(ISDKProtocolAdapter protocolAdapter, Object proxy, String user, String pwd)
	{
//		super(user, pwd);
//		// this.protocolAdapter = protocolAdapter;
//		this.proxy = proxy;
//		this.terminalAAService = new TerminalAAService(protocolAdapter);
	    super(protocolAdapter, proxy, user, pwd);
	    
	    this.terminalAAService = new TE30V1R1C01AAService(protocolAdapter);
	}

	public IDevice getDevice()
	{
		return (IDevice) proxy;
	}

	@Override
	public Object getServiceProxy(Class<?>[] itfs)
	{
		return this.proxy;
	}

	@Override
	public boolean doHeartbeat(String connId)
	{
		try
		{
			prepareSessionId(connId);

			String res = terminalAAService.terminalKeepAlive();
			if (res != null && res.contains("\"success\":1"))
			{
				return true;
			}
		} catch (ProtocolAdapterException e)
		{
			LOGGER.error("", e);
		}

		return false;
	}

	@Override
	public boolean initConn(String connId)
	{
		try
		{
		    LOGGER.debug("terminal initConn " + connId);
			prepareSessionId(connId);
			terminalAAService.terminalLoginRequest();
			String authRes = terminalAAService.terminalAuthorize(
					getLoginUser(), getLoginPassword());
			if (authRes != null && authRes.contains("\"success\":1"))
			{
				return true;
			}
		} catch (ProtocolAdapterException e)
		{
			LOGGER.error("", e);
		}

		return false;
	}

	@Override
	public void destroyConn(String connId)
	{
		prepareSessionId(connId);

		terminalAAService.terminalLogout();
	}

	@Override
	public int getKeepAliveTimes()
	{
		return 3;
	}

	@Override
	public int getKeepAlivePeriod()
	{
		return 50;
	}
}
